package com.googlecode.tabletags.components;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.UIBean;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;

import com.googlecode.tabletags.tagsupport.PagerDisplay;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * Render an HTML pager.
 */
@StrutsTag(name = "pager", tldTagClass = "com.googlecode.tabletags.views.jsp.ui.PagerTag", description = "Renders a pager navigator")
public class Pager extends AbstractURLComponent {
    final public static String TEMPLATE = "pager";
    
    // Pager attributes
    protected String pagerCommand;
    protected String pagerValue;

    /**
     * @see UIBean
     * @param stack
     * @param request
     * @param response
     */
    public Pager(ValueStack stack, HttpServletRequest request,
        HttpServletResponse response) {
        super(stack, request, response);
    }

    /**
     * @see com.opensymphony.webwork.components.UIBean#getDefaultTemplate()
     */
    protected String getDefaultTemplate() {
        return TEMPLATE;
    }

    /**
     * @see com.opensymphony.webwork.components.UIBean#evaluateExtraParams()
     */
    protected void evaluateExtraParams() {
        super.evaluateExtraParams();

        PagerDisplay pagerDisplay = null;

        if(pagerValue != null) {
            pagerDisplay = (PagerDisplay) findValue(pagerValue,
                    PagerDisplay.class);
        }

        if(pagerDisplay != null) {
            addParameter("totalSize", toString(pagerDisplay.getTotalSize()));
            addParameter("pageIndex", toString(pagerDisplay.getPageIndex()));
            addParameter("pageSize", toString(pagerDisplay.getPageSize()));
            addParameter("totalPages", toString(pagerDisplay.getTotalPages()));
            addParameter("hasPreviousPage", new Boolean(pagerDisplay.hasPreviousPage()));
            addParameter("hasNextPage", new Boolean(pagerDisplay.hasNextPage()));
        }

        if(url.indexOf('?') >= 0) {
            url += "&";
        } else {
            url += "?";
        }

        addParameter("url", url);
    }

    /**
     * @param value
     * @return the String representation of the value.
     */
    private String toString(int value) {
        return Integer.toString(value);
    }

    /**
     * @see com.opensymphony.webwork.components.UIBean#getValueClassType()
     */
    protected Class getValueClassType() {
        return PagerDisplay.class;
    }

    @StrutsTagAttribute(description = "the action property for the pager command")
    public void setPagerCommand(String pagerCommand) {
        this.pagerCommand = pagerCommand;
    }

    @StrutsTagAttribute(description = "the action property for the PagerDisplay")
    public void setPagerValue(String pagerValue) {
        this.pagerValue = pagerValue;
    }
}
